﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using Newtonsoft.Json;
using System;

namespace XCI.Core
{
    /// <summary>
    /// 封装布尔消息，消息中封装两个字段：一个表示操作是否成功，一个表示操作消息
    /// </summary>
    public class BoolMessage
    {
        /// <summary>
        /// 成功消息，消息内容为空
        /// </summary>
        public static readonly BoolMessage True = new BoolMessage(true, string.Empty, null);

        /// <summary>
        /// 失败消息，消息内容为空
        /// </summary>
        public static readonly BoolMessage False = new BoolMessage(false, string.Empty, null);

        /// <summary>
        /// 初始化布尔消息
        /// </summary>
        public BoolMessage()
        {
        }

        /// <summary>
        /// 从指定的布尔状态来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        public BoolMessage(bool success)
        {
            this.Success = success;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="msg">状态消息</param>
        public BoolMessage(bool success, string msg)
        {
            this.Success = success;
            this.Msg = msg;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="data">数据对象</param>
        public BoolMessage(bool success, object data)
        {
            this.Success = success;
            this.Data = data;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="msg">状态消息</param>
        /// <param name="data">数据对象</param>
        public BoolMessage(bool success, string msg, object data)
        {
            this.Success = success;
            this.Msg = msg;
            this.Data = data;
        }

        /// <summary>
        /// 合并消息
        /// </summary>
        /// <param name="messages">多个消息</param>
        public static BoolMessage MergeBoolMessage(params BoolMessage[] messages)
        {
            if (messages == null || messages.Length == 0)
            {
                return BoolMessage.True;
            }
            var boolMessage = new BoolMessage(true);
            foreach (var item in messages)
            {
                boolMessage.Success &= item.Success;
                if (!string.IsNullOrWhiteSpace(item.Msg))
                {
                    boolMessage.Msg += $"{item.Msg};";
                }
            }
            return boolMessage;
        }

        /// <summary>
        /// 合并消息
        /// </summary>
        /// <param name="message">多个消息</param>
        /// <param name="separator">分隔符</param>
        public BoolMessage Merge(BoolMessage message, string separator = ";")
        {
            this.Success &= message.Success;
            if (!string.IsNullOrWhiteSpace(message.Msg))
            {
                this.Msg += $"{message.Msg}{separator}";
            }
            return this;
        }

        /// <summary>
        /// 成功状态
        /// </summary>
        [JsonProperty("success")]
        public bool Success { get; set; }

        /// <summary>
        /// 失败状态
        /// </summary>
        [JsonIgnore]
        public bool Fail => !Success;

        /// <summary>
        /// 状态消息
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        [JsonProperty("data")]
        public object Data { get; set; }
    }

    /// <summary>
    /// 封装布尔消息，消息中封装两个字段：一个表示操作是否成功，一个表示操作消息
    /// </summary>
    public class BoolMessage<T>
    {
        /// <summary>
        /// 成功消息，消息内容为空
        /// </summary>
        public static readonly BoolMessage<T> True = new BoolMessage<T>(true);

        /// <summary>
        /// 失败消息，消息内容为空
        /// </summary>
        public static readonly BoolMessage<T> False = new BoolMessage<T>(false);

        /// <summary>
        /// 初始化布尔消息
        /// </summary>
        public BoolMessage()
        {

        }

        /// <summary>
        /// 从指定的布尔状态来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        public BoolMessage(bool success)
        {
            this.Success = success;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="msg">状态消息</param>
        public BoolMessage(bool success, string msg)
        {
            this.Success = success;
            this.Msg = msg;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="data">数据对象</param>
        public BoolMessage(bool success, T data)
        {
            this.Success = success;
            this.Data = data;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="success">布尔状态</param>
        /// <param name="msg">状态消息</param>
        /// <param name="data">数据对象</param>
        public BoolMessage(bool success, string msg, T data)
        {
            this.Success = success;
            this.Msg = msg;
            this.Data = data;
        }

        /// <summary>
        /// 从指定的布尔状态和状态消息来初始化布尔消息
        /// </summary>
        /// <param name="data">数据对象</param>
        public BoolMessage(T data)
        {
            this.Success = true;
            this.Data = data;
        }

        /// <summary>
        /// 成功状态
        /// </summary>
        [JsonProperty("success")]
        public bool Success { get; set; }

        /// <summary>
        /// 失败状态
        /// </summary>
        [JsonIgnore]
        public bool Fail => !Success;

        /// <summary>
        /// 状态消息
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        [JsonProperty("data")]
        public T Data { get; set; }
    }
}